import java.awt.Rectangle;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JScrollBar;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

/**
 * This class is global data UI
 */
public class GData extends JPanel {
	

	private JComboBox mngBox = null;
	private JLabel mngOptLabel = null;
	/**
	 * title for the current window  
	 */
	private JLabel lblTitle = null;
	/**
	 * select button 
	 */
	private JButton btnSelect = null;
	/**
	 * back button 
	 */
	public JButton btnBack = null;
	/**
	 * help button 
	 */
	public JButton btnHelp = null;
	/**
	 * admin main screen
	 */
	private AdminUI admUI = null;
	/**
	 * manUI main screen
	 */
	private ManUI manUI = null;
	/**
	 * secUI main screen
	 */
	private SecUI secUI = null;
	/**
	 * ManagerUI main screen
	 */
	private ManagerUI ManagerUI = null;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	 *  combo box for  dynamic id
	 */
	private JComboBox CmbID = null;
	private String selection[] = new String[2];   // {type,id}


	public GData() {
		super();
		initialize();
	}


	private void initialize() {
		curPanel = this;
		selection[0]="Students";
		
		mngOptLabel = new JLabel();
		mngOptLabel.setBounds(new Rectangle(28, 71, 48, 19));
		mngOptLabel.setText("Type:");
	
		btnSelect = new JButton();
		btnSelect.setBounds(new Rectangle(150, 207, 95, 25));
		btnSelect.setText("Select");

		
		lblTitle = new JLabel("Choose a data to see");
		lblTitle.setBounds(145, 29, 146, 14);

		
		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(mngOptLabel);
		this.add(lblTitle);
		this.add(btnSelect(),null);
		this.add(getMngBox(), null);
		this.add(getCmbID(),null);
		
		btnBack = new JButton("Back");
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose the type of data you want to see","and click on 'Select' button ");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		this.setVisible(true);
		
		btnBack.setBounds(191, 296, 86, 25);
		this.add(btnBack,null);
		
		JLabel lblSelectWhichOn = new JLabel("Select which one to see:");
		lblSelectWhichOn.setBounds(28, 113, 147, 16);
		add(lblSelectWhichOn);
		this.setVisible(true);
		
	}
	
	private JComboBox getMngBox() {
		if (mngBox == null) {
			mngBox = new JComboBox();
			mngBox.setBounds(new Rectangle(196, 68, 95, 25));
			mngBox.addItem("Students");
			mngBox.addItem("Teachers");
			mngBox.addItem("Course");
			mngBox.addItem("Parents");
			mngBox.addItem("Class");
			mngBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					selection[0] = mngBox.getSelectedItem().toString(); 
					SQL s = new SQL(mngBox.getSelectedItem().toString(),36);
					s.accept();
					Object msg = s.getMsg();
					LinkedList<String> list = (LinkedList<String>)msg;
					String str = list.pollFirst();
					CmbID.removeAllItems();
					while (str != null)
					{
						CmbID.addItem(str);
						str = list.pollFirst();
					}

				}
			});
		}
		return mngBox;
	}
	
	private JButton btnSelect() {
		this.btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			    selection[0]= mngBox.getSelectedItem().toString();
				selection[1] = CmbID.getSelectedItem().toString();
				System.out.println("this is what you selected: type: "+selection[0]+" id: "+ selection[1]);
				
				if (selection[0].equals("Students"))
				{
					Student S = new Student(selection[1]);
					S.DBSHOW();
				}
				if (selection[0].equals("Teachers"))
				{
					
					Teacher T = new Teacher(selection[1]);
					T.DBSHOW();
				}
				
				if (selection[0].equals("Parents"))
				{
					Parent P = new Parent(selection[1]);
					P.DBSHOW();
				}
				
				if (selection[0].equals("Class"))
				{
					Cls CLASS = new Cls(selection[1]);
					CLASS.DBSHOW();	
				}		
				
				if (selection[0].equals("Course"))
				{
					Course Co = new Course(selection[1]);
					Co.DBSHOW();	
				}	
			}
		});
		return btnSelect;
	}
	private JComboBox getCmbID() {
		if (CmbID == null) {
			CmbID = new JComboBox();
			CmbID.setBounds(196, 113, 95, 25);
			SQL s = new SQL(mngBox.getSelectedItem().toString(),36);
			s.accept();
			Object msg = s.getMsg();
			LinkedList<String> list = (LinkedList<String>)msg;
			String str = list.pollFirst();
			while (str != null)
			{
				CmbID.addItem(str);
				str = list.pollFirst();
			}
			}
			CmbID.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
				}
			});
		
		return CmbID;
	}
}
